package mo.leetcode_top;

public class L38 {

    public String countAndSay(int n) {
        String res = "1";
        while (--n > 0) {
            StringBuilder cur = new StringBuilder();
            for (int i = 0; i < res.length(); i++) {
                int cnt = 1;
                while (i+1 < res.length() && res.charAt(i) == res.charAt(i+1)) {
                    cnt++;
                    i++;
                }
                cur.append(cnt).append(res.charAt(i));
            }
            res = cur.toString();
        }
        return res;
    }

    public static void main(String[] args) {
        L38 test = new L38();
        System.out.println(test.countAndSay(4));
    }

}
